package online.longzipeng.mywebdemo;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.mywebdemo.entity.Comment;
import online.longzipeng.mywebdemo.entity.PushMessage;
import online.longzipeng.mywebdemo.entity.User;
import online.longzipeng.mywebdemo.enums.BaseEnum;
import online.longzipeng.mywebdemo.enums.PushMessageTypeEnum;
import online.longzipeng.mywebdemo.mapper.UserMapper;
import online.longzipeng.mywebdemo.service.CommentService;
import online.longzipeng.mywebdemo.service.PushMessageService;
import online.longzipeng.mywebdemo.utils.MockUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lzp
 * @description: 模拟数据
 * @Date: 2022/12/9
 */
@Slf4j
@SpringBootTest(classes = MyWebDemoApplication.class,
		webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MockDataDemo {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PushMessageService pushMessageService;
	@Autowired
	private CommentService commentService;


	/**
	 * 用 50 个用户 对同一个基础对象 模拟一批评论数据 1000 条
	 */
	@Test
	public void mockComment() throws ScriptException {
		// 先查询50个用户
		List<User> users = userMapper.selectList(Wrappers.<User>lambdaQuery().last("LIMIT 50"));
		List<Comment> comments = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			// 随机一个用户
			User user1 = users.get(RandomUtil.randomInt(0,users.size()));
			User user2 = users.get(RandomUtil.randomInt(0,users.size()));
			comments.add(
					new Comment(null,new Date(),user1.getId(), MockUtils.cparagraph(),1,user2.getId())
			);
		}
		commentService.saveBatch(comments);
	}

	/**
	 * 取出 50个用户 对 100个动态随机进行 点赞、收藏、投币、转发 操作 添加1000条记录
	 */
	@Test
	public void mockPushMessage() {
		// 先查询50个用户
		List<User> users = userMapper.selectList(Wrappers.<User>lambdaQuery().last("LIMIT 50"));
		List<PushMessage> pushMessages = new ArrayList<>();
		/// 添加1000条记录
		for (int i = 0; i < 1000; i++) {
			// 随机取一个用户
			User user = users.get(RandomUtil.randomInt(0, users.size()));
			/// 随机动作【即随机消息类型】
			PushMessageTypeEnum pushMessageTypeEnum = BaseEnum.valueOf(PushMessageTypeEnum.class, RandomUtil.randomInt(1, 5));
			// 随机动态id
			Integer dynamicId = RandomUtil.randomInt(1, 101);
			pushMessages.add(
					new PushMessage(null, pushMessageTypeEnum.getDescription() + "动态",
							new Date(), pushMessageTypeEnum.getValue(), 1, user.getId(), dynamicId, 0));
		}
		pushMessageService.saveBatch(pushMessages);
	}

}
